package com.yunhe.policyfulfillment.policyful.controller;

import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.mybatis.PageResult;
import com.yunhe.policyfulfillment.policyful.domain.Appeal;
import com.yunhe.policyfulfillment.policyful.model.AppealCondition;
import com.yunhe.policyfulfillment.policyful.model.AppealDistribute;
import com.yunhe.policyfulfillment.policyful.model.AppealVO;
import com.yunhe.policyfulfillment.policyful.model.AppealView;
import com.yunhe.policyfulfillment.policyful.service.AppealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * @ClassName: AppealController
 * @Description:
 * @author: GongDaCheng
 * @Date: 14:24 2021/8/24
 */
@Api(value = "Appeal", description = "诉求维护", tags = {"Model_Appeal"})
@RestController
@RequestMapping(value = "/appeal")
public class AppealController {

    @Autowired
    AppealService appealService;

    @ApiOperation("新增诉求")
    @PostMapping("/createAppeal")
    public ResponseEntity<ResultObject> createAppeal(@RequestBody AppealVO appealVO) throws ArgumentErrorException {
        int count = appealService.createAppeal(appealVO);
        return new ResponseEntity<>(new ResultObject<>(count), HttpStatus.OK);
    }
    
    @ApiOperation("根据id删除诉求")
    @DeleteMapping("/deleteAppeal")
    public ResponseEntity<ResultObject> deleteAppeal(@RequestParam(value = "id",required = true) int id) throws ArgumentErrorException {
        int count = appealService.deleteAppeal(id);
        return new ResponseEntity<>(new ResultObject<>(count), HttpStatus.OK);
    }
    
    @ApiOperation("根据id查看诉求")
    @GetMapping("/viewAppealById")
    public ResponseEntity<ResultObject<AppealView>> viewAppealById(@RequestParam(value = "appealId",required = true) long appealId){
        return new ResponseEntity<>(new ResultObject(appealService.viewAppealById(appealId)),HttpStatus.OK);
    }

    @ApiOperation("根据条件查询诉求")
    @PostMapping("/getAppealByCondition")
    public ResponseEntity<ResultObject<List<Appeal>>> getAppealByCondition(@RequestBody AppealCondition appealCondition) throws ParseException {
        PageResult result = appealService.getAppealByCondition(appealCondition);
        return new ResponseEntity<>(new ResultObject(result),HttpStatus.OK);
    }

    @ApiOperation("公开诉求")
    @PutMapping("/publicAppeal")
    public ResponseEntity<ResultObject> publicAppeal(@RequestParam(value = "id",required = true) long id){
        appealService.publicAppeal(id);
        return new ResponseEntity<>(new ResultObject(),HttpStatus.OK);
    }

    /*@ApiOperation("分发诉求")
    @PutMapping("/distributeAppeal")
    public ResponseEntity<ResultObject> distributeAppeal(@RequestParam(value = "appealId", required = true) long appealId,
                                                         @RequestParam(value = "handleOrg", required = true) long handleOrg,
                                                         @RequestParam(value = "deadLine", required = true)@DateTimeFormat(pattern = "yyyy-MM-dd") Date deadLine) throws ArgumentErrorException {
        appealService.distributeAppeal(appealId, handleOrg, deadLine);
        return new ResponseEntity<>(new ResultObject(),HttpStatus.OK);
    }*/

    @ApiOperation("分发诉求")
    @PutMapping("/distributeAppeal")
    public ResponseEntity<ResultObject> distributeAppeal(@RequestBody AppealDistribute appealDistribute) throws ArgumentErrorException {
        appealService.distributeAppeal(appealDistribute.getAppealId(), appealDistribute.getHandleOrgId(), appealDistribute.getDeadLine());
        return new ResponseEntity<>(new ResultObject(),HttpStatus.OK);
    }

    /**
     * targetStatus传值
     * 回复 : "03"
     * 退回 : "01"
     * 中止 : "04"
     */
    @ApiOperation("处理诉求")
    @PutMapping("/handleAppeal")
    public ResponseEntity<ResultObject> handleAppeal(@RequestParam(value = "appealId", required = true) long appealId,
                                                     @RequestParam(value = "suggest", required = true) String suggest,
                                                      @RequestParam(value = "targetStatus", required = true) String targetStatus) throws ArgumentErrorException {
        appealService.handleAppeal(appealId, suggest, targetStatus);
        return new ResponseEntity<>(new ResultObject(),HttpStatus.OK);
    }

    @ApiOperation("催办")
    @PutMapping("/urgeAppeal")
    public ResponseEntity<ResultObject> urgeAppeal(@RequestParam(value = "appealId", required = true) long appealId) throws ArgumentErrorException {
        appealService.urgeAppeal(appealId);
        return new ResponseEntity<>(new ResultObject(),HttpStatus.OK);
    }
}
